package com.dduggs.controller;

import com.dduggs.object.*;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author Created by CJS on 2018/4/8
 * @ClassName:
 * @Description:
 */
@Controller
public class TestController {

    @RequestMapping(value = "baseType.do")
    @ResponseBody
    public String baseType(@RequestParam(value = "age") int age) {

        return "age" + age;
    }

    @RequestMapping(value = "baseType2.do")
    @ResponseBody
    public String baseType(@RequestParam(value = "age", required = false) Integer age) {

        return "age" + age;
    }

    //    http://localhost:8080/array.do?name=aaa&name=bbb&name=ccc
    @RequestMapping(value = "array.do")
    @ResponseBody
    public String array(String[] name) {

        StringBuffer stringBuffer = new StringBuffer();
        for (String item : name) {
            stringBuffer.append(item).append(" ");
        }
        return stringBuffer.toString();
    }

    //    http://localhost:8080/object.do?name=aaa&age=10
    //    http://localhost:8080/object.do?name=aaa&age=10&contactInfo.phone=10086
    @RequestMapping(value = "object.do")
    @ResponseBody
    public String object(User user) {

        return user.toString();
    }


//    @RequestMapping(value = "object.do")
//    @ResponseBody
//    public String object(User user, Admin admin) {
//
//        return user.toString() + admin.toString();
//    }

    //当前controller有效
    @InitBinder
    public void initUser(WebDataBinder binder) {

        binder.setFieldDefaultPrefix("user.");
    }

    @InitBinder
    public void initAdmin(WebDataBinder binder) {

        binder.setFieldDefaultPrefix("admin.");
    }

    //    http://localhost:8080/list.do?users[0].name=aaa&users[0].age=10&users[0].contactInfo.phone=10086
    @RequestMapping(value = "list.do")
    @ResponseBody
    public String object(List<User> userList) {
//    public String list(UserListForm userList) {

        return userList.toString();
    }

    //    http://localhost:8080/set.do?users[0].name=aaa&users[0].age=10&users[0].contactInfo.phone=10086
    @RequestMapping(value = "set.do")
    @ResponseBody
    public String set(UserSetForm userSetForm) {

        return userSetForm.toString();
    }

    //    http://localhost:8080/map.do?users['key1'].name=aaa&users['key1'].age=10&users['key1'].contactInfo.phone=10086
    @RequestMapping(value = "map.do")
    @ResponseBody
    public String map(UserMapForm userMapForm) {

        return userMapForm.toString();
    }

    @RequestMapping(value = "json.do")
    @ResponseBody
    public String json(@RequestBody User user) {

        return user.toString();
    }


    @RequestMapping(value = "xml.do")
    @ResponseBody
    public String xml(@RequestBody Admin admin) {

        return admin.toString();
    }


    @RequestMapping(value = "date1.do")
    @ResponseBody
    public String date1(Date dateFmt) {

        return dateFmt.toString();
    }

    @RequestMapping(value = "date2.do")
    @ResponseBody
    public String date2(Date date) {

        return date.toString();
    }

//    @InitBinder("dateFmt")
//    public void initDate1(WebDataBinder binder) {
//
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyy-MM-dd"), true));
//    }

    public static void main(String[] args) {
        AtomicInteger integer=new AtomicInteger(2);
        AtomicLong l=new AtomicLong(2);
        LongAdder longAdder=new LongAdder();
        integer.addAndGet(1);
    }
}
